home *** CD-ROM | disk | FTP | other *** search
/ Qoole for Quake / Qoole for Quake (USA) / Qoole for Quake (USA).bin / Tutorial / HTML / QUBE.ZIP / SRC / CURS.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-05  |  4.5 KB  |  244 lines

  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <stdarg.h>
  5. #include <ctype.h>
  6. #include "curs.h"
  7.  
  8. /*
  9.  *  General-purpose cursor control routines for QBSP, VIS, and LIGHT.
  10.  *  These are used so that cursor control can be done compatibly
  11.  *  with other architectures (namely UNIX).  This version implements
  12.  *  cursor control with CONIO.H (available with Turbo C and with DJGPP).
  13.  */
  14.  
  15. #include <conio.h>
  16.  
  17. int backtable[8] = { 0, 4, 2, 6, 1, 5, 3, 7 };
  18. int foretable[8] = { 0, 12, 10, 14, 9, 13, 11, 15 };
  19.  
  20. struct vidbuf {
  21.     int x, y, width, height;
  22.     void *memory;
  23. };       
  24.  
  25. int ScrnWidth, ScrnHeight;
  26.  
  27. void InitText(void)
  28. {
  29.     textmode(3);            /* Reset to normal text mode */
  30.     _setcursortype(_NOCURSOR);    /* Hide the cursor */
  31.     clrscr();            /* Clear the screen */
  32.     gotoxy(1, 1);            /* Home the cursor */
  33.  
  34.     ScrnWidth = 80;
  35.     ScrnHeight = 24;        /* If it were 25, it would scroll, damn it. */
  36. }
  37.  
  38. void MoveCurs(int x, int y)
  39. {
  40.     gotoxy(x+1, y+1);    /* X and Y are zero-based */
  41. }
  42.  
  43. void GetCurs(int *x, int *y)
  44. {
  45.     *x = wherex() - 1;    /* X and Y are zero-based */
  46.     *y = wherey() - 1;
  47. }
  48.  
  49. void SetForeColor(int color)
  50. {
  51.     if (color > 7 || color < 0) color = 7;
  52.     textcolor(foretable[color]);
  53. }
  54.  
  55. void SetBackColor(int color)
  56. {
  57.     if (color > 7 || color < 0) color = 7;
  58.     textbackground(backtable[color]);
  59. }
  60.  
  61. void CPrintf(char *format, ...)
  62. {
  63.     register char c;
  64.     register char *ptr, *src;
  65.         int x, y;
  66.         char text[1024];
  67.     char text2[1024];
  68.         va_list v;
  69.  
  70.     va_start(v, format);
  71.     vsprintf(text, format, v);
  72.  
  73.     GetCurs(&x, &y);
  74.  
  75.     for (ptr = text2, src = text; *src != '\0'; src++) {
  76.         c = *src;
  77.  
  78.         if (c == '$') {
  79.             switch (*++src) {
  80.             case '$':
  81.                 *ptr++ = '$';
  82.                                 break;
  83.                         case 'a':
  84.                 *ptr = '\0';
  85.                 cputs(ptr = text2);
  86.                                 RingBell();
  87.                                 break;
  88.                         case 'n':
  89.                 *ptr = '\0';
  90.                 cputs(ptr = text2);
  91.                                 MoveCurs(x, ++y);
  92.                                 break;
  93.             case 'f':
  94.                 *ptr = '\0';
  95.                 cputs(ptr = text2);
  96.                 SetForeColor(toupper(*++src)-'0');
  97.                 break;
  98.             case 'b':
  99.                 *ptr = '\0';
  100.                 cputs(ptr = text2);
  101.                 SetBackColor(toupper(*++src)-'0');
  102.                 break;
  103.             case 'c':
  104.                 *ptr = '\0';
  105.                 cputs(ptr = text2);
  106.                 SetForeColor(toupper(*++src)-'0');
  107.                 SetBackColor(toupper(*++src)-'0');
  108.                 break;
  109.                         }
  110.                 }
  111.         else if (c >= 32 && c <= 126)
  112.             *ptr++ = c;
  113.     }
  114.     *ptr = '\0';
  115.     cputs(ptr = text2);
  116. }
  117.  
  118. void RingBell(void)
  119. {
  120.     putch('\a');
  121. }
  122.  
  123. void FillBox(int x, int y, int width, int height, char c)
  124. {
  125.     register int i;
  126.     char boxdata[256];
  127.  
  128.     for (i = 0; i < width; i++)
  129.         boxdata[i] = c;
  130.     boxdata[i] = '\0';
  131.  
  132.     for (i = 0; i < height; i++) {
  133.         gotoxy(x+1, i+1+y);
  134.         cputs(boxdata);
  135.         }
  136. }
  137.  
  138. void DrawBox(int x, int y, int width, int height)
  139. {
  140.     register int i;
  141.  
  142.         gotoxy(x+1, y+1);
  143.     cputs("┌");
  144.     for (i = 0; i < width - 2; i++)
  145.         cputs("─");
  146.     cputs("┐");
  147.  
  148.     for (i = 1; i < height-1; i++) {
  149.         gotoxy(x+1, i+1+y);
  150.         cputs("│");
  151.         gotoxy(x+width, i+1+y);
  152.         cputs("│");
  153.         }
  154.  
  155.     gotoxy(x+1, y+height);
  156.     cputs("└");
  157.     for (i = 0; i < width - 2; i++)
  158.         cputs("─");
  159.     cputs("┘");
  160. }
  161.  
  162. void DrawFilledBox(int x, int y, int width, int height)
  163. {
  164.     register int i;
  165.         char boxdata[256];
  166.  
  167.     for (i = 0; i < width; i++)
  168.         boxdata[i] = ' ';
  169.     boxdata[0] = '│';
  170.     boxdata[i-1] = '│';
  171.         boxdata[i] = '\0';
  172.  
  173.         gotoxy(x+1, y+1);
  174.     cputs("┌");
  175.     for (i = 0; i < width - 2; i++)
  176.         cputs("─");
  177.     cputs("┐");
  178.  
  179.     for (i = 1; i < height-1; i++) {
  180.         gotoxy(x+1, i+1+y);
  181.         cputs(boxdata);
  182.         }
  183.  
  184.     gotoxy(x+1, y+height);
  185.     cputs("└");
  186.     for (i = 0; i < width - 2; i++)
  187.         cputs("─");
  188.     cputs("┘");
  189. }
  190.  
  191. void DrawHLine(int x, int y, int length)
  192. {
  193.     register int i;
  194.  
  195.     gotoxy(x+1, y+1);
  196.     for (i = 0; i < length; i++)
  197.         cputs("─");
  198. }
  199.  
  200. void DrawVLine(int x, int y, int length)
  201. {
  202.     register int i;
  203.  
  204.     for (i = 0; i < length; i++) {
  205.         gotoxy(x+1, i+1+y);
  206.         cputs("│");
  207.     }
  208. }
  209.  
  210. void CopyText(int x1, int y1, int width, int height, int x2, int y2)
  211. {
  212.     void *mem = malloc(width*height*2);
  213.  
  214.     gettext(x1-1, y1-1, x1+width, y1+height, mem);
  215.     puttext(x2-1, y2-1, x2+width, y2+height, mem);
  216. }
  217.  
  218. int IsKey(void)
  219. {
  220.     return(kbhit());
  221. }
  222.  
  223. int WaitKey(void)
  224. {
  225.     return(getch());
  226. }
  227.  
  228. int GetKey(void)
  229. {
  230.     if (kbhit()) return(getch());
  231.     else return(0);
  232. }
  233.  
  234. void HideCurs(void)
  235. {
  236.     _setcursortype(_NOCURSOR);
  237. }
  238.  
  239. void ShowCurs(void)
  240. {
  241.     _setcursortype(_NORMALCURSOR);
  242. }
  243.  
  244.